package cn.appsys.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.pojo.DevUser;
import cn.appsys.service.developer.AppCategoryService;
import cn.appsys.service.developer.AppInfoService;
import cn.appsys.service.developer.AppVersionService;
import cn.appsys.service.developer.DataDictionaryService;
import cn.appsys.service.developer.DevUserService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;
@Controller
@RequestMapping(value = "/dev")
public class UserController {
	// 服务类接口
	@Resource
	private	DevUserService devUserService;	
	
	@Resource
	private AppInfoService appInfoService;
	
	@Resource 
	private DataDictionaryService dictionaryService;
	
	@Resource 
	private AppCategoryService appCategoryService;
	
	@Resource
	private AppVersionService appVersionService;
	
	// 首页
		@RequestMapping(value = "/index")
		public String index(HttpServletRequest request) {
			return "index";
		}

		// 登陆方法
		@RequestMapping(value = "/login")
		public String login(HttpServletRequest request) {
			// 跳转至后台登陆页面
			request.getSession().removeAttribute("error");
			return "devlogin";
		}

		// 登陆验证方法
		@RequestMapping(value = "/dologin")
		public String dologin(HttpServletRequest request,HttpSession session,@RequestParam("devCode") String devCode,
				@RequestParam("devPassword") String devPassword) {
			/* 调用登陆方法根据返回值确定是否登陆成功 */
			DevUser user = null;
			try {
				user = devUserService.login(devCode,devPassword);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(null != user){//登录成功
				//放入session
				session.setAttribute(Constants.DEV_USER_SESSION, user);
				//页面跳转（main.jsp）
				return "/developer/main";
			}else{
				//页面跳转（login.jsp）带出提示信息--转发
				request.setAttribute("error", "用户名或密码不正确");
				return "devlogin";
			}

		}

		
		/* 注销方法 */
		@RequestMapping(value = "/logout")
		public String logout(HttpServletRequest request) {
			/* 移除会话中的用户并重定向至首页 */
			request.getSession().removeAttribute("devUserSession");
			return "redirect:../index.jsp";
		}
		
		//查询列表 方法
		@RequestMapping(value="/list")
		public String getAppInfoList(Model model,HttpSession session,
								@RequestParam(value="querySoftwareName",required=false) String querySoftwareName,
								@RequestParam(value="queryStatus",required=false) String _queryStatus,
								@RequestParam(value="queryCategoryLevel1",required=false) String _queryCategoryLevel1,
								@RequestParam(value="queryCategoryLevel2",required=false) String _queryCategoryLevel2,
								@RequestParam(value="queryCategoryLevel3",required=false) String _queryCategoryLevel3,
								@RequestParam(value="queryFlatformId",required=false) String _queryFlatformId,
								@RequestParam(value="pageIndex",required=false) String pageIndex){
			
			
			Integer devId = ((DevUser)session.getAttribute(Constants.DEV_USER_SESSION)).getId();
			List<AppInfo> appInfoList = null;
			List<DataDictionary> statusList = null;
			List<DataDictionary> flatFormList = null;
			List<AppCategory> categoryLevel1List = null;//列出一级分类列表，注：二级和三级分类列表通过异步ajax获取
			List<AppCategory> categoryLevel2List = null;
			List<AppCategory> categoryLevel3List = null;
			//页面容量
			int pageSize = Constants.pageSize;
			//当前页码
			Integer currentPageNo = 1;
			
			if(pageIndex != null){
				try{
					currentPageNo = Integer.valueOf(pageIndex);
				}catch (NumberFormatException e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			Integer queryStatus = null;
			if(_queryStatus != null && !_queryStatus.equals("")){
				queryStatus = Integer.parseInt(_queryStatus);
			}
			Integer queryCategoryLevel1 = null;
			if(_queryCategoryLevel1 != null && !_queryCategoryLevel1.equals("")){
				queryCategoryLevel1 = Integer.parseInt(_queryCategoryLevel1);
			}
			Integer queryCategoryLevel2 = null;
			if(_queryCategoryLevel2 != null && !_queryCategoryLevel2.equals("")){
				queryCategoryLevel2 = Integer.parseInt(_queryCategoryLevel2);
			}
			Integer queryCategoryLevel3 = null;
			if(_queryCategoryLevel3 != null && !_queryCategoryLevel3.equals("")){
				queryCategoryLevel3 = Integer.parseInt(_queryCategoryLevel3);
			}
			Integer queryFlatformId = null;
			if(_queryFlatformId != null && !_queryFlatformId.equals("")){
				queryFlatformId = Integer.parseInt(_queryFlatformId);
			}
			
			//总数量（表）
			int totalCount = 0;
			try {
				totalCount = appInfoService.getAppInfoCount(querySoftwareName, queryStatus, queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3, queryFlatformId, devId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//总页数
			PageSupport pages = new PageSupport();
			pages.setCurrentPageNo(currentPageNo);
			pages.setPageSize(pageSize);
			pages.setTotalCount(totalCount);
			int totalPageCount = pages.getTotalPageCount();
			//控制首页和尾页
			if(currentPageNo < 1){
				currentPageNo = 1;
			}else if(currentPageNo > totalPageCount){
				currentPageNo = totalPageCount;
			}
			try {
				appInfoList = appInfoService.getAppInfoList(querySoftwareName, queryStatus, queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3, queryFlatformId, devId, currentPageNo, pageSize);
				statusList = this.getDataDictionaryList("APP_STATUS");
				flatFormList = this.getDataDictionaryList("APP_FLATFORM");
				categoryLevel1List = appCategoryService.getAppCategoryListByParentId(null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			model.addAttribute("appInfoList", appInfoList);
			model.addAttribute("statusList", statusList);
			model.addAttribute("flatFormList", flatFormList);
			model.addAttribute("categoryLevel1List", categoryLevel1List);
			model.addAttribute("pages", pages);
			model.addAttribute("queryStatus", queryStatus);
			model.addAttribute("querySoftwareName", querySoftwareName);
			model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
			model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
			model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);
			model.addAttribute("queryFlatformId", queryFlatformId);
			
			//二级分类列表和三级分类列表---回显
			if(queryCategoryLevel2 != null && !queryCategoryLevel2.equals("")){
				categoryLevel2List = getCategoryList(queryCategoryLevel1.toString());
				model.addAttribute("categoryLevel2List", categoryLevel2List);
			}
			if(queryCategoryLevel3 != null && !queryCategoryLevel3.equals("")){
				categoryLevel3List = getCategoryList(queryCategoryLevel2.toString());
				model.addAttribute("categoryLevel3List", categoryLevel3List);
			}
	
			return "developer/appinfolist";
		}
		//获取一级分类方法
		@RequestMapping(value="/queryCategoryLevel1")
		@ResponseBody
		public Object queryCategoryLevel1(HttpServletRequest request) throws Exception {
			List<AppCategory> list=appCategoryService.getAppCategoryListByParentId(null);
			return JSON.toJSONString(list);
		}
		
		//获取二级分类方法
		@RequestMapping(value="/queryCategoryLevel2")
		@ResponseBody
		public Object queryCategoryLevel2(HttpServletRequest request) throws Exception {
			Integer parentId=Integer.parseInt(request.getParameter("pid"));
			List<AppCategory> list=appCategoryService.getAppCategoryListByParentId(parentId);
			return JSON.toJSONString(list);
		}

		//获取三级分类方法
		@RequestMapping(value="/queryCategoryLevel3")
		@ResponseBody
		public Object queryCategoryLevel3(HttpServletRequest request) throws Exception {
			Integer parentId=Integer.parseInt(request.getParameter("pid"));
			List<AppCategory> list=appCategoryService.getAppCategoryListByParentId(parentId);
			return JSON.toJSONString(list);
		}
		

		public List<DataDictionary> getDataDictionaryList(String typeCode){
			List<DataDictionary> dataDictionaryList = null;
			try {
				dataDictionaryList = dictionaryService.getDataDictionaryList(typeCode);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return dataDictionaryList;
		}
		
		/**
		 * 根据typeCode查询出相应的数据字典列表
		 * @param pid
		 * @return
		 */
		@RequestMapping(value="/datadictionarylist.json",method=RequestMethod.GET)
		@ResponseBody
		public List<DataDictionary> getDataDicList (@RequestParam String tcode){
			return this.getDataDictionaryList(tcode);
		}
		
		/**
		 * 根据parentId查询出相应的分类级别列表
		 * @param pid
		 * @return
		 */
		@RequestMapping(value="/categorylevellist.json",method=RequestMethod.GET)
		@ResponseBody
		public List<AppCategory> getAppCategoryList (@RequestParam String pid){
			if(pid.equals("")) pid = null;
			return getCategoryList(pid);
		}
		
		public List<AppCategory> getCategoryList (String pid){
			List<AppCategory> categoryLevelList = null;
			try {
				categoryLevelList = appCategoryService.getAppCategoryListByParentId(pid==null?null:Integer.parseInt(pid));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return categoryLevelList;
		}
		//详情查看方法
		@RequestMapping(value="/appview")
		public String appview(HttpServletRequest request,@RequestParam(value="id") Integer id) {
			//将详情实体类写入会话中并跳转至详情页面
			try {
				request.getSession().setAttribute("appInfo", appInfoService.getAppInfo(id, null));
				request.getSession().setAttribute("appVersionList", appVersionService.getAppVersionList(id));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "developer/appinfoview";
		}
		/**
		 * 增加方法
		 */
		@RequestMapping(value="/appinfoadd",method=RequestMethod.GET)
		public String appinfoadd(@ModelAttribute("appInfo")AppInfo appInfo){
			return "developer/appinfoadd";
			
		}
		/**
		 * 获取所属平台列表
		 * @param appInfo
		 * @param session
		 * @param request
		 * @param attach
		 * @return
		 */
		@RequestMapping(value="/getfaltform")
		@ResponseBody
		public Object getfaltform(HttpServletRequest request){
			List<DataDictionary> list=null;
			try {
				list = dictionaryService.getDataDictionaryList("APP_FLATFORM");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return JSON.toJSONString(list);
		}
		/**
		 * 验证APK名称
		 * @param appInfo
		 * @param session
		 * @param request
		 * @param attach
		 * @return
		 */
		@RequestMapping(value="/getApkName")
		@ResponseBody
		public Object getApkName(@RequestParam(value="APKName",required=false)String APKName,HttpServletRequest request) throws Exception {
			//获取传过来的APP名称
			//如果为空则返回状态为空
			if(APKName==null||APKName=="") {
				return "empty";
			}
			//判断此APP名称是否已经存在
			AppInfo app=appInfoService.getAppInfo(null, APKName);
			//不存在则返回不存在，存在则返回存在
			if(app!=null) {
				return "exist";
			}
			return "noexist";
		}
		@RequestMapping(value="/softwareName")
		@ResponseBody
		public Object getsoftwareName(@RequestParam(value="softwareName",required=false)String softwareName,HttpServletRequest request) throws Exception {
			//获取传过来的APP名称
			//如果为空则返回状态为空
			if(softwareName==null||softwareName=="") {
				return "empty";
			}
			//判断此APP名称是否已经存在
			AppInfo app=appInfoService.getAppInfo(null, softwareName);
			//不存在则返回不存在，存在则返回存在
			if(app!=null) {
				return "exist";
			}
			return "noexist";
		}
		@RequestMapping(value="/appinfoaddsave",method=RequestMethod.POST)
		public String addSave(AppInfo appInfo,HttpSession session,HttpServletRequest request,
						@RequestParam(value="a_logoPicPath",required= false) MultipartFile attach){		
			
			String logoPicPath =  null;
			String logoLocPath =  null;
			if(!attach.isEmpty()){
				String path = request.getSession().getServletContext().getRealPath("statics"+java.io.File.separator+"uploadfiles");
				String oldFileName = attach.getOriginalFilename();//原文件名
				String prefix = FilenameUtils.getExtension(oldFileName);//原文件后缀
				int filesize = 500000;
				if(attach.getSize() > filesize){//上传大小不得超过 50k
					request.setAttribute("fileUploadError", Constants.FILEUPLOAD_ERROR_4);
					return "developer/appinfoadd";
	            }else if(prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png") 
				   ||prefix.equalsIgnoreCase("jepg") || prefix.equalsIgnoreCase("pneg")){//上传图片格式
					 String fileName = appInfo.getAPKName() + ".jpg";//上传LOGO图片命名:apk名称.apk
					 File targetFile = new File(path,fileName);
					 if(!targetFile.exists()){
						 targetFile.mkdirs();
					 }
					 try {
						attach.transferTo(targetFile);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						request.setAttribute("fileUploadError", Constants.FILEUPLOAD_ERROR_2);
						return "developer/appinfoadd";
					} 
					 logoPicPath = request.getContextPath()+"/statics/uploadfiles/"+fileName;
					 logoLocPath = path+File.separator+fileName;
				}else{
					request.setAttribute("fileUploadError", Constants.FILEUPLOAD_ERROR_3);
					return "developer/appinfoadd";
				}
			}
			appInfo.setCreatedBy(((DevUser)session.getAttribute(Constants.DEV_USER_SESSION)).getId());
			appInfo.setCreationDate(new Date());
			appInfo.setLogoPicPath(logoPicPath);
			appInfo.setLogoLocPath(logoLocPath);
			appInfo.setDevId(((DevUser)session.getAttribute(Constants.DEV_USER_SESSION)).getId());
			appInfo.setStatus(1);
			try {
				if(appInfoService.add(appInfo)){
					return "redirect:/dev/list";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "developer/appinfoadd";
		}
		/**
		 * 增加appversion信息（跳转到新增页面，版本页面）
		 */
		@RequestMapping(value="/appversionadd",method=RequestMethod.GET)
		public String addVersion(@RequestParam(value="id")Integer appId,HttpServletRequest request){
			AppVersion appVersion=new AppVersion();
			appVersion.setAppId(appId);
			//将APP的历史版本查询出来并存入会话中在跳转至添加版本信息页面
			request.getSession().setAttribute("appVersion", appVersion);
			try {
				request.getSession().setAttribute("appVersionList",appVersionService.getAppVersionById(appId));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "developer/appversionadd";
		}
		/**
		 * 保存增加的app版本信息
		 */
		@RequestMapping(value="/addversionsave",method=RequestMethod.POST)
		public String addVersionSave(AppVersion appVersion,HttpSession session,HttpServletRequest request,
							@RequestParam(value="a_downloadLink",required= false) MultipartFile attach ){		
			String downloadLink =  null;
			String apkLocPath = null;
			String apkFileName = null;
			if(!attach.isEmpty()){
				String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
				String oldFileName = attach.getOriginalFilename();//原文件名
				String prefix = FilenameUtils.getExtension(oldFileName);//原文件后缀
				if(prefix.equalsIgnoreCase("apk")){//apk文件命名：apk名称+版本号+.apk
					 String apkName = null;
					 try {
						apkName = appInfoService.getAppInfo(appVersion.getAppId(),null).getAPKName();
					 } catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					 }
					 if(apkName == null || "".equals(apkName)){
						 return "redirect:/dev/appversionadd?id="+appVersion.getAppId()
								 +"&error=error1";
					 }
					 apkFileName = apkName + "-" +appVersion.getVersionNo() + ".apk";
					 File targetFile = new File(path,apkFileName);
					 if(!targetFile.exists()){
						 targetFile.mkdirs();
					 }
					 try {
						attach.transferTo(targetFile);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return "redirect:/dev/appversionadd?id="+appVersion.getAppId()
								 +"&error=error2";
					} 
					downloadLink = request.getContextPath()+"/statics/uploadfiles/"+apkFileName;
					apkLocPath = path+File.separator+apkFileName;
				}else{
					return "redirect:/dev/appversionadd?id="+appVersion.getAppId()
							 +"&error=error3";
				}
			}
			appVersion.setCreatedBy(((DevUser)session.getAttribute(Constants.DEV_USER_SESSION)).getId());
			appVersion.setCreationDate(new Date());
			appVersion.setDownloadLink(downloadLink);
			appVersion.setApkLocPath(apkLocPath);
			appVersion.setApkFileName(apkFileName);
			try {
				if(appVersionService.appsysadd(appVersion)){
					return "redirect:/dev/list";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "redirect:/dev/appversionadd?id="+appVersion.getAppId();
		}
		/**
		 * 删除appversion信息
		 */
		@RequestMapping(value="/delapp.json")
		@ResponseBody
		public Object delApp(@RequestParam String id){
			HashMap<String, String> resultMap = new HashMap<String, String>();
			if(StringUtils.isNullOrEmpty(id)){
				resultMap.put("delResult", "notexist");
			}else{
				try {
					if(appInfoService.appsysdeleteAppById(Integer.parseInt(id)))
						resultMap.put("delResult", "true");
					else
						resultMap.put("delResult", "false");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return JSONArray.toJSONString(resultMap);
		}
		//修改APP信息方法
		@RequestMapping(value="/appinfomodify")
		public String appinfomodify(HttpServletRequest request) throws Exception {
			//获取APPid之后查询到APP的详细信息并存入会话中 再跳转至修改页面
			Integer id=Integer.parseInt(request.getParameter("id"));
			AppInfo appInfo=appInfoService.getAppInfo(id, null);
			request.getSession().setAttribute("appInfo",appInfo);
			return "developer/appinfomodify";
		}
		//保存修改APP信息方法
		@RequestMapping(value="/appinfomodifysave",method=RequestMethod.POST)
		public String modifySave(AppInfo appInfo,HttpSession session,HttpServletRequest request,
								@RequestParam(value="attach",required= false) MultipartFile attach){		
			String logoPicPath =  null;
			String logoLocPath =  null;
			String APKName = appInfo.getAPKName();
			if(!attach.isEmpty()){
				String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
				String oldFileName = attach.getOriginalFilename();//原文件名
				String prefix = FilenameUtils.getExtension(oldFileName);//原文件后缀
				int filesize = 500000;
				if(attach.getSize() > filesize){//上传大小不得超过 50k
	            	 return "redirect:/dev/appinfomodify?id="+appInfo.getId()
							 +"&error=error4";
	            }else if(prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png") 
				   ||prefix.equalsIgnoreCase("jepg") || prefix.equalsIgnoreCase("pneg")){//上传图片格式
					 String fileName = APKName + ".jpg";//上传LOGO图片命名:apk名称.apk
					 File targetFile = new File(path,fileName);
					 if(!targetFile.exists()){
						 targetFile.mkdirs();
					 }
					 try {
						attach.transferTo(targetFile);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return "redirect:/dev/appinfomodify?id="+appInfo.getId()
								+"&error=error2";
					} 
					 logoPicPath = request.getContextPath()+"/statics/uploadfiles/"+fileName;
					 logoLocPath = path+File.separator+fileName;
	            }else{
	            	return "redirect:/dev/appinfomodify?id="+appInfo.getId()
							 +"&error=error3";
	            }
			}
			appInfo.setModifyBy(((DevUser)session.getAttribute(Constants.DEV_USER_SESSION)).getId());
			appInfo.setModifyDate(new Date());
			appInfo.setLogoLocPath(logoLocPath);
			appInfo.setLogoPicPath(logoPicPath);
			try {
				if(appInfoService.modify(appInfo)){
					return "redirect:/dev/list";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "developer/appinfomodify";
		}
		
		
		@RequestMapping(value="/delfile")
		@ResponseBody
		public String delfile(HttpServletRequest request) throws Exception {
			//获取APPID
			Integer id=Integer.parseInt(request.getParameter("id"));
			//根据删除成功返回相应的字符串
			if(appInfoService.deleteAppLogo(id)) {
				return "success";
			}
			return "failed";
		}
		/**
		 * 上架下架
		 */
		@RequestMapping(value="/{appid}/sale",method=RequestMethod.PUT)
		@ResponseBody
		public Object sale(@PathVariable String appid,HttpSession session){
			HashMap<String, Object> resultMap = new HashMap<String, Object>();
			Integer appIdInteger = 0;
			try{
				appIdInteger = Integer.parseInt(appid);
			}catch(Exception e){
				appIdInteger = 0;
			}
			resultMap.put("errorCode", "0");
			resultMap.put("appId", appid);
			if(appIdInteger>0){
				try {
					DevUser devUser = (DevUser)session.getAttribute(Constants.DEV_USER_SESSION);
					AppInfo appInfo = new AppInfo();
					appInfo.setId(appIdInteger);
					appInfo.setModifyBy(devUser.getId());
					if(appInfoService.appsysUpdateSaleStatusByAppId(appInfo)){
						resultMap.put("resultMsg", "success");
					}else{
						resultMap.put("resultMsg", "success");
					}		
				} catch (Exception e) {
					resultMap.put("errorCode", "exception000001");
				}
			}else{
				resultMap.put("errorCode", "param000001");
			}
			return resultMap;
		}
		//修改APP版本信息方法
		@RequestMapping(value="/appversionmodify")
		public String appversionmodify(@RequestParam(value="vid")Integer versionId,@RequestParam(value="aid")Integer appId,HttpServletRequest request){
			AppVersion appVersion;
			try {
				appVersion = appVersionService.getAppVersionById(versionId);
				request.getSession().setAttribute("appVersion", appVersion);
				request.getSession().setAttribute("appVersionList", appVersionService.getAppVersionList(appId));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "developer/appversionmodify";
		}
		/**
		 * 保存修改后的appVersion
		 * @param appVersion
		 * @param session
		 * @return
		 */
		@RequestMapping(value="/appversionmodifysave",method=RequestMethod.POST)
		public String modifyAppVersionSave(AppVersion appVersion,HttpSession session,HttpServletRequest request,
						@RequestParam(value="attach",required= false) MultipartFile attach){	
			
			String downloadLink =  null;
			String apkLocPath = null;
			String apkFileName = null;
			if(!attach.isEmpty()){
				String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
				String oldFileName = attach.getOriginalFilename();//原文件名
				String prefix = FilenameUtils.getExtension(oldFileName);//原文件后缀
				if(prefix.equalsIgnoreCase("apk")){//apk文件命名：apk名称+版本号+.apk
					 String apkName = null;
					 try {
						apkName = appInfoService.getAppInfo(appVersion.getAppId(),null).getAPKName();
					 } catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					 }
					 if(apkName == null || "".equals(apkName)){
						 return "redirect:/dev/appversionmodify?vid="+appVersion.getId()
								 +"&aid="+appVersion.getAppId()
								 +"&error=error1";
					 }
					 apkFileName = apkName + "-" +appVersion.getVersionNo() + ".apk";
					 File targetFile = new File(path,apkFileName);
					 if(!targetFile.exists()){
						 targetFile.mkdirs();
					 }
					 try {
						attach.transferTo(targetFile);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return "redirect:/dev/appversionmodify?vid="+appVersion.getId()
								 +"&aid="+appVersion.getAppId()
								 +"&error=error2";
					} 
					downloadLink = request.getContextPath()+"/statics/uploadfiles/"+apkFileName;
					apkLocPath = path+File.separator+apkFileName;
				}else{
					return "redirect:/dev/appversionmodify?vid="+appVersion.getId()
							 +"&aid="+appVersion.getAppId()
							 +"&error=error3";
				}
			}
			appVersion.setModifyBy(((DevUser)session.getAttribute(Constants.DEV_USER_SESSION)).getId());
			appVersion.setModifyDate(new Date());
			appVersion.setDownloadLink(downloadLink);
			appVersion.setApkLocPath(apkLocPath);
			appVersion.setApkFileName(apkFileName);
			try {
				if(appVersionService.modify(appVersion)){
					return "redirect:/dev/list";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "developer/appversionmodify";
		}
		//删除版本信息
		@RequestMapping(value="/delfileByVersion")
		@ResponseBody
		public String delfileByVersion(HttpServletRequest request,@RequestParam(value="id")Integer id)throws Exception{
			if(appVersionService.deleteApkFile(id)){
				return "success";
			}
			return "failed";
		}
		
}